define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, $timeout, Distributor_order, $stateParams, GoPage, $window,GoDialog) {
		$scope.scope = new Object();
		$scope.scope.type = 'finish';
		$scope.scope.type_items = [{id:"finish",name:"已完成"},
		{id:"nopayed",name:"待付款"},{id:"ship",name:"待发货"},{id:"dead",name:"已取消"},
		{id:"confirm",name:"待确认"},{id:"refunded",name:"已退货"}];
		$scope.scope.type_select = {id:0,name:"请选择订单状态"};
		$scope.id = $stateParams.id;
		$scope.scope.number = '';
		$scope.scope.name = '';
		$scope.time = '';
		$scope.scope.time = '';
		$scope.order_count = 0;
		$scope.order_money = 0;
		$scope.start_time = '';
		$scope.end_time = '';
		$scope.scope.start_time = '';
		$scope.scope.end_time = '';
		$scope.input = new Object();
		$scope.input.nPage = 1;
		$scope.input.start_date = '';
		$scope.input.end_date = '';
		$scope.count = null;
		$scope.doSelectType = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.scope.type_items,$scope.scope.type_select,function(data){
				$scope.scope.type = data.id;
				$scope.scope.type_select = data;
			});
		}
		$scope.$on('$ionicView.afterEnter', function(){
			var lan = 0; //这是状态栏，只有ios的APP才有
		var tab = 0;
		var bars = document.getElementById("nav-bar-title");
		var bar = ((bars != undefined) ? 44 : 0);
		if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) { lan = 20; }
		//整体高度
		var all = $window.innerHeight;
		var disorder_top1 = document.getElementById("disorder-top1").clientHeight;
		$scope.disorder_myObj = { //滚动条可视范围
			"height": all - lan - bar - tab - disorder_top1-20 + "px"
		};	
		$scope.init_data();
			},false)
		//画面加载
		$scope.isLoad = false;
		$scope.init_data = function() {
			Distributor_order.order_list(1, $scope.id, $scope.scope.number, $scope.scope.name, $scope.time, $scope.scope.type, function(data) {

				if(data != null) {
					$scope.data = data;
					$scope.order_count = data.order_count;
					$scope.order_money = data.order_money;
					if(data.order_money == null)
						$scope.order_money = 0;
				} else {
					$scope.data = new Object();
					$scope.order_count = 0;
					$scope.order_money = 0;
				}

				$scope.input.nPage = 1;

			})
		}
		
		$scope.search = function() {
			if($scope.scope.start_time != '')
				$scope.start_time = (new Date($scope.input.start_date)).getTime() / 1000;
			if($scope.scope.end_time != '')
				$scope.end_time = (new Date($scope.input.end_date)).getTime() / 1000;
//			console.log($scope.start_time);
//			console.log($scope.end_time);
			if($scope.end_time != '' && $scope.start_time != '')
				$scope.time = $scope.start_time + '~' + $scope.end_time;
			Distributor_order.order_list(1, $scope.id, $scope.scope.number, $scope.scope.name, $scope.time, $scope.scope.type, function(data) {
//				console.log(1);
				if(data != null) {
					$scope.data = data;
					$scope.order_count = data.order_count;
					$scope.order_money = data.order_money;
					if(data.order_money == null)
						$scope.order_money = 0;
				} else {
					$scope.data = new Object();
					$scope.order_count = 0;
					$scope.order_money = 0;
				}
				$scope.input.nPage = 1;
//				console.log(data);
			})
		}
		//防止重复加载
		//		$scope.isLoad = false;
		//下拉刷新
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			if($scope.input.start_date != '')
				$scope.start_time = (new Date($scope.input.start_date)).getTime() / 1000;
			if($scope.input.end_date != '')
				$scope.end_time = (new Date($scope.input.end_date)).getTime() / 1000;
			if($scope.end_time != '' && $scope.start_time != '')
				$scope.time = $scope.start_time + '~' + $scope.end_time;
			//条件查询
			Distributor_order.order_list(1, $scope.id, $scope.scope.number, $scope.scope.name, $scope.time, $scope.scope.type, function(data) {
				if(data != null) {
					$scope.order_count = data.order_count;
					$scope.order_money = data.order_money;
					$scope.data = data;
					$scope.input.nPage = 1;
					if(data.order_money == null)
						$scope.order_money = 0;
				}
				if(data == null) {
					$scope.order_count = 0;
					$scope.order_money = 0;
					$scope.data = new Object();
					$scope.input.nPage = 1;
				}
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false; //可以加载
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;

			if($scope.input.start_date != '')
				$scope.start_time = (new Date($scope.input.start_date)).getTime() / 1000;
			if($scope.input.end_date != '')
				$scope.end_time = (new Date($scope.input.end_date)).getTime() / 1000;
			if($scope.end_time != '' && $scope.start_time != '')
				$scope.time = $scope.start_time + '~' + $scope.end_time;
			if($scope.data.order == undefined) {
				$scope.input.nPage = 0;

			} else
			if($scope.data.page.count <= $scope.data.order.length) {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				if($scope.data.page.count<$scope.input.nPage*10){
					$scope.input.nPage -=1;
					$scope.count = $scope.input.nPage *10;
				}else{
					$scope.count = null;
				}
			}
			//条件查询
			Distributor_order.order_list($scope.input.nPage + 1, $scope.id, $scope.scope.number, $scope.scope.name, $scope.time, $scope.scope.type, function(data) {
//				console.log(data);
				if(data.order != null) {
					//数据合并
					if($scope.data.order != undefined) {
						if($scope.count!=null)
						$scope.data.order.splice($scope.count,$scope.data.page.count-$scope.count);
						$scope.data.order = $scope.data.order.concat(data.order);
						$scope.input.nPage = $scope.input.nPage + 1;
						
					} else {
						$scope.order_count = data.order_count;
						$scope.order_money = data.order_money;
						$scope.data = data;
						$scope.input.nPage = 1;
					}

				}
			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false; //可以加载
				}, 1500);

			});
		};
		//页面跳转
		$scope.show_detail = function(orderNo, store_id) {
			GoPage.go_states("myagentdistributor_order", { "order_id": orderNo, "store_id": $scope.id, "user_type_id": 4 });
		};
		$scope.onshow_first = function(store_id) {
			GoPage.go_states("first", { "store_id": store_id });
		}
		//		$scope.init_data();
	}

	controller.$inject = ['$scope', '$timeout', 'Distributor_order', '$stateParams', 'GoPage', '$window','GoDialog'];

	return app.registerController("distributor_orderctrl",controller);
});